package com.djp.computerServer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseService<T extends JpaRepository<Y,Integer>,Y> {
    @Autowired
    protected T dao;
    protected List<Y> list;

    public List<Y> getAll() {
        return dao.findAll();
    }

    public List<Y> add(Y y) {
        list = new ArrayList<>();
        list.add(dao.save(y));
        return list;
    }

    public List<Y> getOne(Y y) {
        //System.out.println(y.toString());
        return dao.findAll(Example.of(y));
    }

    public boolean delete(Y y) {
        try {
            System.out.println(y.toString());
            dao.delete(y);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public List<Y> update(Y y) {
        list = new ArrayList<>();
        list.add(dao.save(y));
        return list;
    }
}
